package io.renren.modules.jxgkphone.controller;

import io.renren.common.exception.RRException;
import io.renren.common.utils.R;
import io.renren.modules.jxgk.utils.UUidCreater;
import io.renren.modules.jxgkphone.dao.ClassCourseMapper;
import io.renren.modules.jxgkphone.entity.ClassCourse;
import io.renren.modules.jxgkphone.service.ClassAttendanceService;
import io.renren.modules.jxgkphone.service.ClassDiaryService;
import io.renren.modules.jxgkphone.tempEntity.TempAttend;
import io.renren.modules.jxgkphone.tempEntity.TempUpdateAttend;
import io.renren.modules.oss.cloud.OSSFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/12/27 0027.
 */
@RestController
@RequestMapping("/classAttendances")
public class ClassAttendancesController {
    @Autowired
    ClassAttendanceService classAttendanceService;
    @Autowired
    ClassCourseMapper classCourseMapper;
    @Autowired
    ClassDiaryService classDiaryService;
    public String CurrentDate(){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String Currentdate=df.format(new Date());
        return Currentdate;
    }

    //第二个：查询->更新（插入）->查询/classAttendances/UpdateClassStatus
    @RequestMapping("/UpdateClassStatus")
    public R UpdateClassStatus(@RequestBody List<TempUpdateAttend> tempUpdateAttends){
        UUidCreater u=new UUidCreater();
        //获取课程时间
        String courseTime=classDiaryService.CourseTime(tempUpdateAttends.get(0).getCid());
        if(u.compare_date(courseTime,CurrentDate())==1){
           return R.error("拒绝访问当前日期");
       } else if(tempUpdateAttends.size()!=0) {
            List<TempAttend> stuAD = classAttendanceService.UpdateClassStatus(tempUpdateAttends);
            if(stuAD==null&&stuAD.size()==0){
                return    R.error("您已经修改过了，不能再次修改");
            }else {
                //返回出勤状态的同时还要返回班委操作考勤的次数
                ClassCourse classCourse=classCourseMapper.getCourseById(tempUpdateAttends.get(0).getCid());
                return R.ok().put("stuAD", stuAD).put("isAttendance",classCourse.getIsAttendance());
            }
        }
            return   R.error("您的数据有误");
    }

    //上传照片
    @RequestMapping("UploadImage")
        public R upload(HttpServletRequest request, @Param("file") MultipartFile file) throws Exception {
        if (file==null||file.isEmpty()) {
            throw new RRException("上传文件不能为空");

        }
        //上传文件
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String path = OSSFactory.build().uploadSuffix(file.getBytes(), suffix);
        return R.ok().put("data", path);
    }

    //获取同学个人的打卡状态


}
